package myJs.projectCps.plsda3D

import com.softwaremill.quicklens._
import myJs.cps._
import myJs.tool.Tool._
import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._
import myJs.api.Api
import myJs.tool.Tool
import myJs.user.FeatureSelectionTool
import shared.pojo.MissionTool.CheckFileExist

import scala.collection.immutable.SeqMap

/** Created by yz on 3/11/2020
  */
object LayoutCp {

  val plotO = Cp.plotO

  case class Props(
      basicData: plotO.LayoutData,
      basicDataChange: (plotO.LayoutData => plotO.LayoutData) => SyncIO[Unit],
      id: Int,
      idStr:String
  ) {
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>

    val (componentMap, setComponentMap) = useState(SeqMap("1"->"C1","2"->"C2","3"->"C3"))

    object FTool {

      def vChange[T](f: PathLazyModify[plotO.LayoutData, T])(g: String => T) = (v: String) => {
        props.basicDataChange {
          f.setTo(g(v))
        }
      }

      def strChange(f: PathLazyModify[plotO.LayoutData, String]) = FTool.vChange(f)(x => x)

      def refreshComponentMap(idStr: String) = {
        val fileName = "PLSDA_Score.csv"
        val finalFileName = FeatureSelectionTool.getFinalFileName(idStr, fileName)
        val request = CheckFileExist.Request(missionId = Tool.getMissionId, fileName = finalFileName)
        Api.missionTool.getHeaders(request).flatMapSync { response =>
          val curPcaMap = response.map { x =>
            val id = x.replaceAll("^C", "")
            (id, x)
          }.toSeqMap
          setComponentMap(curPcaMap)
        }
      }

    }

    val basicData = props.basicData
    val id = props.id
    val idStr = props.idStr

    useEffect(
      {
        if(id!=0){
          FTool.refreshComponentMap(idStr)
        }else IO.unit
      },
      List(id,idStr)
    )

    Fragment(
      SelectInlineCp
        .Props(
          value = basicData.x,
          label = messages("xAxis"),
          name = s"x",
          optionMap = componentMap,
          onChange = FTool.strChange(modifyLens[plotO.LayoutData](_.x)),
          labelWidth = 4,
          inputWidth = 4
        )
        .render,
      SelectInlineCp
        .Props(
          value = basicData.y,
          label = messages("yAxis"),
          name = s"y",
          optionMap = componentMap,
          onChange = FTool.strChange(modifyLens[plotO.LayoutData](_.y)),
          labelWidth = 4,
          inputWidth = 4
        )
        .render,
      SelectInlineCp
        .Props(
          value = basicData.z,
          label = messages("zAxis"),
          name = s"z",
          optionMap = componentMap,
          onChange = FTool.strChange(modifyLens[plotO.LayoutData](_.z)),
          labelWidth = 4,
          inputWidth = 4
        )
        .render,
      BetweenInlineCp
        .Props(
          label = messages("picSizeWidthHeight"),
          startName = "width",
          start = basicData.width,
          startChange = FTool.strChange(modifyLens[plotO.LayoutData](_.width)),
          endName = "height",
          end = basicData.height,
          endChange = FTool.strChange(modifyLens[plotO.LayoutData](_.height))
        )
        .render
    )

  }

}
